package com.compty.paint.util;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;

import com.compty.core.framework.context.spring.SpringContextHolder;

/**
 * @Todo:清除二级缓存
 * @author: zhuqh
 * @CreateTime:2011-9-21 下午03:51:46
 */
public class ClearSecondLevelCache extends HttpServlet{
	
	private static final long serialVersionUID = 6815207273205989501L;
	
	private static  SessionFactory sf;
	
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		
		sf = SpringContextHolder.getBean("sessionFactory");
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	
	@SuppressWarnings("unchecked")
	public static void evictSecondLevelCache() {
		Map<String, CollectionMetadata> roleMap = sf.getAllCollectionMetadata();
		for (String roleName : roleMap.keySet()) {
			sf.evictCollection(roleName);
		}

		Map<String, ClassMetadata> entityMap = sf.getAllClassMetadata();
		for (String entityName : entityMap.keySet()) {
			sf.evictEntity(entityName);
		}

		sf.evictQueries();
	}

}
